﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HRMSys.Model;
using HRMSys.DAL;

namespace HRMSys.UI.SystemMgr
{
    /// <summary>
    /// DepartmentListUI.xaml 的交互逻辑
    /// </summary>
    public partial class DepartmentListUI : Window
    {
        public DepartmentListUI()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            DepartmentDAL dal = new DepartmentDAL();
            Department[] departs = dal.GetAll(true);
            gridDepartList.ItemsSource = departs;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DepartmentDAL dal = new DepartmentDAL();
            Department[] departs = dal.GetAll(true);
            gridDepartList.ItemsSource = departs;
        }

        /// <summary>
        /// 新增按纽点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddDepart_Click(object sender, RoutedEventArgs e)
        {
            DepartmentEditUI editUI = new DepartmentEditUI();
            editUI.IsInsert = true;
            if (editUI.ShowDialog() == true)
            {
                this.LoadData();
            }
        }

        /// <summary>
        /// 编辑按纽点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditDepart_Click(object sender, RoutedEventArgs e)
        {
            object selectedItem = gridDepartList.SelectedItem;
            if (selectedItem == null)
            {
                MessageBox.Show("请选择要编辑的数据!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            Guid departId = ((Department)selectedItem).Id;
            DepartmentEditUI editUI = new DepartmentEditUI();
            editUI.IsInsert = false;
            editUI.EditingId = departId;
            if (editUI.ShowDialog() == true)
            {
                this.LoadData();
            }
        }

        /// <summary>
        /// 新增或修改部门信息
        /// </summary>
        /// <param name="isInsert"></param>
        private void AddOrEdit(bool isInsert) 
        {
            if (isInsert == false) 
            {
                object selectedItem = gridDepartList.SelectedItem;
                if (selectedItem == null) 
                {
                    MessageBox.Show("请选择要编辑的数据!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }
            }
            DepartmentEditUI editUI = new DepartmentEditUI();
            editUI.IsInsert = isInsert;
            if (editUI.ShowDialog() == true)
            {
                this.LoadData();
            }
        }
    }
}
